package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.pojo.*;
import com.hqyj.dao.PaperDao;
import com.hqyj.service.IPaperQuestionRelService;
import com.hqyj.service.IPaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-09-15
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperDao, Paper> implements IPaperService {

    @Resource
    PaperDao paperDao;
    //注入试卷和试题中间表的service
    @Autowired
    IPaperQuestionRelService iPaperQuestionRelService;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Transactional
    @Override
    public ResultVo paperCreate(Paper p) {
        ResultVo rv = new ResultVo();
        //验证试卷是否重名
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_name",p.getPaperName());
        Paper pp = paperDao.selectOne(queryWrapper);
        if (pp != null){
            rv.setInfo("试卷名称重复");
            return rv;
        }
        //试卷新增
        int num = paperDao.insert(p);
        System.err.println("p:" + p);
        if (num > 0){
            //查询中间表需要的试卷Id
//            queryWrapper.eq("paper_name",p.getPaperName());
//            Paper paper = paperDao.selectOne(queryWrapper);
            //把quIds取出分割
            String[] quIds = p.getQuIds().split(",");
            //创建一个中间表对象集合
            List<PaperQuestionRel> list = new LinkedList<>();

            for (int i = 0; i < quIds.length;i ++ ){
                PaperQuestionRel pqr = new PaperQuestionRel();
                pqr.setPaperId(p.getPaperId());
                pqr.setQuestionId(Integer.parseInt(quIds[i]));
                list.add(pqr);
            }
            //批量插入
            boolean is = iPaperQuestionRelService.saveBatch(list);
            if (is){
                rv.setSuccess(true);
                rv.setInfo("试卷生成成功");
            }else {
                rv.setInfo("试卷生成失败");
            }
        }else {
            rv.setInfo("试卷生成失败");
        }
        return rv;
    }

    @Override
    public ResultVo queryPaperByPaperName(String paperName) {
        ResultVo rv = new ResultVo();
         List<MyPaper> paperList = paperDao.queryPaperByPaperName(paperName);
         Paper paper = new Paper();
         paper.setPaperList(paperList);
         if (paperList != null){
             rv.setSuccess(true);
             rv.setInfo("试卷查询成功");
             rv.setData(paper);
         }else {
             rv.setInfo("试卷查询失败");
         }
        return rv;
    }

    @Override
    public ResultVo queryByName(String pareName) {
       //定义key
        String key = "exam:" + pareName;
        //判断缓存中是否有数据
        Object obj = redisTemplate.opsForValue().get(key);
        Paper p = null;
        if (obj == null){
            p = paperDao.queryByName(pareName);
            System.out.println(p);
            //存入缓存
            redisTemplate.opsForValue().set(key,p,4, TimeUnit.HOURS);

        }else {
            //缓存中取数据
            p =(Paper) obj;
        }


        ResultVo rv = new ResultVo();
        if (p != null){
            rv.setSuccess(true);
            rv.setData(p);
        }else {
            rv.setInfo("查询失败");
        }
        return rv;
    }

    @Override
    public ResultVo countScore(Paper paper) {
       ResultVo rv = new ResultVo();
       //定义key
        String key = "exam:" + paper.getPaperName();
        //从缓存中取出试卷信息
        Paper p = (Paper) redisTemplate.opsForValue().get(key);
        //建立单选、多选、判断的正确答案的集合
        List<PaperQuestion> signList = new LinkedList<>();
        List<PaperQuestion> moreList = new LinkedList<>();
        List<PaperQuestion> judgeList = new LinkedList<>();
        for (PaperQuestion pq : p.getList()){
            if (pq.getQuType().equals("单选题")){
                signList.add(pq);
            }else if (pq.getQuType().equals("多选题")){
                moreList.add(pq);
            }else  {
                judgeList.add(pq);
            }
        }

        //把考生答案转换成数组类型
        String[] signArr = paper.getSa().split(",");
        String[] moreArr = paper.getMa().split(",");
        String[] judgeArr = paper.getJa().split(",");

        //计算总分
        int score = 0;
        //评选单选题答案
        for (int i = 0; i < signList.size() ; i++){
            if (signList.get(i).getQuAnswer().equals(signArr[i])){
                score += signList.get(i).getQuScore();
            }
        }
        //评选多选题答案
        for (int i = 0; i < moreList.size() ; i++){
            if (moreList.get(i).getQuAnswer().equals(moreArr[i])){
                score += moreList.get(i).getQuScore();
            }
        }
        //评选判断题答案
        for (int i = 0; i < judgeList.size() ; i++){
            if (judgeList.get(i).getQuAnswer().equals(judgeArr[i])){
                score += judgeList.get(i).getQuScore();
            }
        }
        rv.setSuccess(true);
        System.out.println("====score:" + score);
        rv.setData(score);

        return rv;
    }


}
